#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

#include "addform.h"


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置表格
    ui->tableWidget->setColumnCount(8);
    ui->tableWidget->setHorizontalHeaderLabels({"学号", "姓名", "昵称", "性别", "年龄", "入学时间", "所属学院", "操作"});

    this->manager = new QNetworkAccessManager(this);
    this->manager->get(QNetworkRequest(QUrl("http://127.0.0.1:8088/school/api/v1.0/student")));
    connect(manager, &QNetworkAccessManager::finished, [this](QNetworkReply *reply){
        // 得到的返回结果是 jsonStr  --->  jsonObj
        QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
        QJsonObject rootObj = jsonDoc.object();

        QJsonArray ary = rootObj.value("data").toArray();

        ui->tableWidget->setRowCount(ary.size());

        int i = 0;
        for (QJsonValue v : ary)
        {
            int j = 0;
            QJsonObject tmp = v.toObject();
            ui->tableWidget->setItem(i, j++, new QTableWidgetItem(QString::number(tmp.value("sno").toInt())));
            ui->tableWidget->setItem(i, j++, new QTableWidgetItem(tmp.value("sname").toString()));
            ui->tableWidget->setItem(i, j++, new QTableWidgetItem(tmp.value("snickname").toString()));
            ui->tableWidget->setItem(i, j++, new QTableWidgetItem(tmp.value("sgender").toString()));
            ui->tableWidget->setItem(i, j++, new QTableWidgetItem(QString::number(tmp.value("sage").toInt())));
            ui->tableWidget->setItem(i, j++, new QTableWidgetItem(tmp.value("stime").toString()));
            ui->tableWidget->setItem(i, j++, new QTableWidgetItem(tmp.value("dname").toString()));


            QWidget *w = new QWidget;
            QHBoxLayout *hBox = new QHBoxLayout(w);

            QPushButton *delBtn = new QPushButton("删除", this);
            delBtn->resize(60, 40);
            QPushButton *editBtn = new QPushButton("编辑", this);
            editBtn->resize(60, 40);

            // 设置水平布局（hBox）的margin; 如果不设置，按钮会被挤变形
            hBox->setMargin(2);

            hBox->addWidget(delBtn);
            hBox->addWidget(editBtn);

            ui->tableWidget->setCellWidget(i++, j, w);
            ui->tableWidget->setColumnWidth(j, 180);


            // 给删除按钮绑定信号和槽
            connect(delBtn, &QPushButton::clicked, [this, tmp](){
                QString url = QString("http://127.0.0.1:8088/school/api/v1.0/student/%1").arg(tmp.value("sno").toInt());
                this->manager->deleteResource(QNetworkRequest(QUrl(url)));
            });

        }

    });

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    AddForm *addForm = new AddForm;
    addForm->setAttribute(Qt::WA_DeleteOnClose);
    addForm->show();

    // 链接reload信号和槽，在槽函数中就可以关闭addForm窗口，也能重新发送获取请求
    connect(addForm, &AddForm::reload, [this, addForm](){
        // 重新发送请求，得到最新的数据
        this->manager->get(QNetworkRequest(QUrl("http://127.0.0.1:8088/school/api/v1.0/student")));
        // 关闭AddForm窗口
        addForm->close();
    });
}
